<?php
namespace app\controllers;


use app\common\corelib\LocalUpload;
use app\logic\PaperLogic;
use app\logic\TestLogic;
use yii;
//试题模板管理
class PaperController extends BackendController
{
    //淘淘拼音模板
    public function actionTaotao()
    {
        $paperLogic = new PaperLogic();
        $this->data['breadcrumbs'] = '试题管理';
        return $this->render('index', [
            'paper_type'=>4,
            'paper' => $paperLogic->getPaper(4)
        ]);
    }
    //淘转课堂模板
    public function actionNaughty()
    {
        $paperLogic = new PaperLogic();
        $this->data['breadcrumbs'] = '试题管理';
        return $this->render('index', [
            'paper_type'=>3,
            'paper' => $paperLogic->getPaper(3)
        ]);
    }
    //YCT模板
    public function actionYct()
    {
        $paperLogic = new PaperLogic();
        $this->data['breadcrumbs'] = '试题管理';
        return $this->render('index', [
            'paper_type'=>5,
            'paper' => $paperLogic->getPaper(5)
        ]);
    }
    //HSK模板
    public function actionHsk()
    {
        $paperLogic = new PaperLogic();
        $this->data['breadcrumbs'] = '试题管理';
        return $this->render('index', [
            'paper_type'=>6,
            'paper' => $paperLogic->getPaper(6)
        ]);
    }
    //西语发音模板
    public function actionPronunciation()
    {
        $paperLogic = new PaperLogic();
        $this->data['breadcrumbs'] = '试题管理';
        return $this->render('index', [
            'paper_type'=>7,
            'paper' => $paperLogic->getPaper(7)
        ]);
    }
    //少儿西语模板
    public function actionChildren()
    {
        $paperLogic = new PaperLogic();
        $this->data['breadcrumbs'] = '试题管理';
        return $this->render('index', [
            'paper_type'=>8,
            'paper' => $paperLogic->getPaper(8)
        ]);
    }
    //Dele模板
    public function actionDele()
    {
        $paperLogic = new PaperLogic();
        $this->data['breadcrumbs'] = '试题管理';
        return $this->render('index', [
            'paper_type'=>9,
            'paper' => $paperLogic->getPaper(9)
        ]);
    }
    //添加模板
    public function actionAdd()
    {
        $paperLogic = new PaperLogic();
        if (!Yii::$app->request->post()) {
            $paper_type = Yii::$app->request->get('paper_type', 0);
            if ($paper_type == 4)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/taotao'), ' 添加试题模板' => ''];
            elseif ($paper_type == 3)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/naughty'), ' 添加试题模板' => ''];
            elseif ($paper_type == 5)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/yct'), ' 添加试题模板' => ''];
            elseif ($paper_type == 6)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/hsk'), ' 添加试题模板' => ''];
            elseif ($paper_type == 7)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/pronunciation'), ' 添加试题模板' => ''];
            elseif ($paper_type == 8)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/children'), ' 添加试题模板' => ''];
            elseif ($paper_type == 9)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/dele'), ' 添加试题模板' => ''];
            return $this->render('add', [
                'paper_type' => $paper_type,
                'theme' => '/assets/',
                'actionUrl' => U('paper/add')
            ]);
        } else {
            $params = Yii::$app->request->post('form');
            if($_FILES['paper_pic']['name']){
                LocalUpload::go_upload($_FILES['paper_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $paperLogic->addPaper($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success', '新增试题模板成功');
                if($params['paper_type']==3)
                    jump('paper/naughty');
                elseif($params['paper_type']==4)
                    jump('paper/taotao');
                elseif($params['paper_type']==5)
                    jump('paper/yct');
                elseif($params['paper_type']==6)
                    jump('paper/hsk');
                elseif($params['paper_type']==7)
                    jump('paper/pronunciation');
                elseif($params['paper_type']==8)
                    jump('paper/children');
                elseif($params['paper_type']==9)
                    jump('paper/dele');
            } else {
                Yii::$app->getSession()->setFlash('error', '新增试题模板失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //编辑模板
    public function actionEdit(){
        $paperLogic = new PaperLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('paper_id');
            $params = Yii::$app->request->get('form');
            if(!$params){
                $params = [];
            }
            if(!$id){
                $id = $params['paper_id'];
            }
            $paper = $paperLogic->getById($id);
            if($paper['paper_type']==4)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/taotao'), $paper['paper_name'] => '', '更新' => ''];
            elseif($paper['paper_type']==3)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/naughty'), $paper['paper_name'] => '', '更新' => ''];
            elseif($paper['paper_type']==5)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/yct'), $paper['paper_name'] => '', '更新' => ''];
            elseif($paper['paper_type']==6)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/hsk'), $paper['paper_name'] => '', '更新' => ''];
            elseif($paper['paper_type']==7)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/pronunciation'), $paper['paper_name'] => '', '更新' => ''];
            elseif($paper['paper_type']==8)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/children'), $paper['paper_name'] => '', '更新' => ''];
            elseif($paper['paper_type']==9)
                $this->data['breadcrumbs'] = ['试题管理' => U('paper/dele'), $paper['paper_name'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$paper['paper_pic'])){
                $paper_pic = $paper['paper_pic'];
            }else{
                $paper_pic = '/default.jpg';
            }
            $testLogic = new testLogic();
            $page = Yii::$app->request->get('page', 1);
            $test = $testLogic->getTestByPaper($id,$params,$page);
            return $this->render('edit', [
                'paper' => $paper,
                'paper_pic' => $paper_pic,
                'test'=>$test,
                'theme' => '/assets/',
                'actionUrl' => U('paper/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('paper_id');
            $paper = $paperLogic->getById($id);
            if($_FILES['paper_pic']['name']){
                LocalUpload::go_upload($_FILES['paper_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $paperLogic->editById($id, $params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新试题模板成功');
                if($paper['paper_type']==3)
                    jump('paper/naughty');
                elseif($paper['paper_type']==4)
                    jump('paper/taotao');
                elseif($paper['paper_type']==5)
                    jump('paper/yct');
                elseif($paper['paper_type']==6)
                    jump('paper/hsk');
                elseif($paper['paper_type']==7)
                    jump('paper/pronunciation');
                elseif($paper['paper_type']==8)
                    jump('paper/children');
                elseif($paper['paper_type']==9)
                    jump('paper/dele');
            } else {
                Yii::$app->getSession()->setFlash('error','更新试题模板失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //删除模板
    public function actionDelete(){
        $id = Yii::$app->request->get('paper_id');
        $paperLogic = new PaperLogic();
        $paper = $paperLogic->getById($id);
        $logicRes = $paperLogic->delete($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        if($paper['paper_type']==4)
            return $this->redirect(U('paper/taotao'));
        elseif($paper['paper_type']==3)
            return $this->redirect(U('paper/naughty'));
        elseif($paper['paper_type']==5)
            return $this->redirect(U('paper/yct'));
        elseif($paper['paper_type']==6)
            return $this->redirect(U('paper/hsk'));
        elseif($paper['paper_type']==7)
            return $this->redirect(U('paper/pronunciation'));
        elseif($paper['paper_type']==8)
            return $this->redirect(U('paper/children'));
        elseif($paper['paper_type']==9)
            return $this->redirect(U('paper/dele'));
    }
    //复制模板
    public function actionCopy(){
        $id = Yii::$app->request->get('paper_id');
        $paperLogic = new PaperLogic();
        $paper = $paperLogic->getById($id);
        $logicRes = $paperLogic->copy($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','复制成功');
        } else {
            Yii::$app->getSession()->setFlash('error','复制失败');
        }
        if($paper['paper_type']==4)
            return $this->redirect(U('paper/taotao'));
        elseif($paper['paper_type']==3)
            return $this->redirect(U('paper/naughty'));
        elseif($paper['paper_type']==5)
            return $this->redirect(U('paper/yct'));
        elseif($paper['paper_type']==6)
            return $this->redirect(U('paper/hsk'));
        elseif($paper['paper_type']==7)
            return $this->redirect(U('paper/pronunciation'));
        elseif($paper['paper_type']==8)
            return $this->redirect(U('paper/children'));
        elseif($paper['paper_type']==9)
            return $this->redirect(U('paper/dele'));
    }


}
